package com.cjb.cloudmqtt.mqtt.publish;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cjb.cloudmqtt.mqtt.entity.DeviceStatus;
import com.cjb.cloudmqtt.mqtt.subscribe.MqttConsumerService;
import com.cjb.common.common.cache.RedisCache;
import com.cjb.common.util.Result;
import io.swagger.v3.oas.annotations.Operation;
import jakarta.validation.Valid;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@AllArgsConstructor
@Slf4j
public class SendController {

    private final MqttProviderService providerClient;
    //注入订阅
    private final MqttConsumerService consumerClient;
    /**
     * 注入redis类
     */
    private final RedisCache redisCache;

    @PostMapping("/sendMessage")
    public String sendMessage(@RequestBody String message) {
        try {
            // 解析收到的消息
            JSONObject root = JSON.parseObject(message);
            JSONObject innerMessage = JSON.parseObject(root.getString("message"));

            // 提取收到消息中的具体值
            String deviceId = innerMessage.getString("deviceId");
            String deviceTemperature = innerMessage.getString("deviceTemperature");
            String deviceHumidity = innerMessage.getString("deviceHumidity");
            String deviceBrightness = innerMessage.getString("deviceBrightness");
            String deviceStatus = innerMessage.getString("deviceStatus");
            String temperatureStatus = innerMessage.getString("temperatureStatus");
            String brightnessStatus = innerMessage.getString("brightnessStatus");

            // 构造新的 JSON 对象，包含提取出的值
            JSONObject newMessage = new JSONObject();
            newMessage.put("deviceId", deviceId);
            newMessage.put("deviceTemperature", deviceTemperature);
            newMessage.put("deviceHumidity", deviceHumidity);
            newMessage.put("deviceBrightness", deviceBrightness);
            newMessage.put("deviceStatus", deviceStatus);
            newMessage.put("temperatureStatus", temperatureStatus);
            newMessage.put("brightnessStatus", brightnessStatus);

            // 将新的 JSON 对象转换为字符串
            String modifiedMessage = newMessage.toJSONString();

            // 发布消息
            providerClient.publish(0, true, "hatelys", modifiedMessage);

            return modifiedMessage;
        } catch (Exception e) {
            e.printStackTrace();
            return "发送失败";
        }
    }

    /**
     * 手动连接
     *
     * @Author chenjinbin
     * @Value
     * @Title
     */
    @GetMapping("connected")
    public String connected(){
        consumerClient.connect();
        return "success";
    }

    /**
     *
     *
     * @Author chenjinbin
     * @Value
     * @Title
     */
    @GetMapping("/queryOne")
    @Operation(summary = "查询单个")
    public Result<DeviceStatus> queryOne(){
        String dataString  = (String)redisCache.hGet("message","deviceStatus");
//        String dataString ="{ \"deviceTemperature\":36\",\"deviceHumidity\": \"34\",\"deviceBrightness\":\"32\",\"deviceStatus\":0}";
        /*"{\"deviceTemperature\":36,\"deviceHumidity\": \"34\", \"deviceBrightness\":\"32\",\"deviceStatus\":0}";*/
        System.out.println("数据 {}"+dataString);
        return Result.success(( JSON.parseObject(dataString,DeviceStatus.class)));
    }

}

